import { useStore } from "vuex";
export default function (audio) {
    const store = useStore();
    function getRandomInt(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min)) + min; //不含最大值，含最小值
    }
    function endPlay() {
        if (store.state.PlaySong.PlayTheOrder === "circulation") {

            const songlist = store.state.SongList.songlist;

            for (let index = 0; index < songlist.length; index++) {

                if (songlist[index].id === store.state.PlaySong.presentSong.id) {

                    if (songlist.length - 1 === index) {
                        index = 0;
                        if (songlist.length === 1) {
                            audio.value.play();
                            break;
                        }
                    } else {
                        index = index + 1;
                    }
                    store.dispatch("PlaySong/getSongUrl", songlist[index]);
                    store.dispatch("SongLyric/Lyric", songlist[index].id);
                    break;
                }
            }
        } else if (store.state.PlaySong.PlayTheOrder === "random") {
            if (store.state.SongList.songlist.length !== 1) {
                const random = getRandomInt(0, store.state.SongList.songlist.length - 1);
                const song = store.state.SongList.songlist[random];
                store.dispatch("PlaySong/getSongUrl", song);
                store.dispatch("SongLyric/Lyric", song.id);
            } else {
                audio.value.play();
            }

        } else {
            audio.value.play();
        }
    }

    return endPlay
}